import * as API from '../../api'
import * as types from '../mutation-types'
const login = {
  state: {
    loginList: [],
    loginResult: {},
    userInfo: {},
    wxInfo: {},
    getCodeResult: {},
    registResult: {},

    factoryOptionsSelect: [],
    factoryOptionsData: [],
  },
  getters: {},
  mutations: {
    [types.GET_LOGIN_LIST] (state, data){
      state.loginList = [];
      let admin = data.admin
      let sub = data.sub
      state.loginList = state.loginList.concat(admin)
      state.loginList = state.loginList.concat(sub)
    },
    [types.LOGIN] (state, data){
      state.loginResult = data
    },
    [types.GET_MOBILE_CODE] (state, data){
      state.getCodeResult = data
    },
    [types.GET_USER_INFO] (state,data){
      state.userInfo = data
    },
    [types.GET_WX_INFO] (state,data){
      state.wxInfo = data
    },
    [types.REGISTE] (state,data){
      state.registResult = data
    },
    [types.GET_FACTORY_LIST_SUCCESS] (state, payload) {
      let list = payload.list
      state.factoryOptionsSelect = []
      state.factoryOptionsData = []
      if (list.length > 0){
        state.factoryOptionsSelect.push({value: '0',label: '全部',factoryid: '',factoryname: ''})
        for(var i = 0;i < list.length; i ++){
          let item = list[i]
          state.factoryOptionsSelect.push({value: String(i + 1), label:item.name,factoryid: item.id,factoryname:item.name})
          state.factoryOptionsData.push({value: String(i + 1), label:item.name,factoryid: item.id,factoryname:item.name})
        }
      }
    },
  },
  actions: {
    getLoginList({commit, state, rootState}, params){
      var prom = API.getLoginList(params)
      prom.then(function (data) {
        commit(types.GET_LOGIN_LIST, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    getMobileCode({commit, state, rootState}, params){
      var prom = API.getMobileCode(params)
      prom.then(function (data) {
        commit(types.GET_MOBILE_CODE, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    login({commit, state, rootState}, params){
      var prom = API.login(params)
      prom.then(function (data) {
        commit(types.LOGIN, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    registe({commit, state, rootState}, params){
      var prom = API.regist(params)
      prom.then(function (data) {
        commit(types.REGISTE, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    getUserInfo({commit, state, rootState}, params){
      var prom = API.getUserInfo(params)
      prom.then(function (data) {
        commit(types.GET_USER_INFO, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    getWxInfo({commit, state, rootState}, params){
      var prom = API.getWxInfo(params)
      prom.then(function (data) {
        commit(types.GET_WX_INFO, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    getFactoryList ({commit,state,rootState}, params) {
      let para = {
        action: 'getlist',
        page:1,
        pagesize:1000,
      }
      var prom = API.getFactoryList(para)
      prom.then(function (data) {
        commit(types.GET_FACTORY_LIST_SUCCESS, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },

  }
}
export default login
